
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class misil here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class misil extends Actor
{
    /**
     * Act - do whatever the misil wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    private GreenfootImage image;
    private int delay=0;
    private int direccion;
    private boolean impact=false;
    int vel;
    public misil(int dir )
    {
        direccion=dir;
        image=getImage();
        vel=5;
        if(direccion==-1)
        {
        image.mirrorHorizontally();
        }//image.scale(30,30);
        setImage(image);
       
    }
    public misil(int direc, boolean especial)
    {
        direccion=direc;
        vel=10;
        setImage("Bullet3.png");
        
    }
    public void act() 
    {
      this.setLocation(getX()+(direccion*vel),getY());
      checkImpact();
      if(getX()>getWorld().getWidth()-10 || getX()<10 || impact==true)
      
      getWorld().removeObject(this);
       
       
    }    
    
    public void checkImpact()
    {
        if(((FlagWarWorld)getWorld()).getInmunidad()==false)
        {
      if (getOneIntersectingObject(PlayerShot.class) != null || getOneIntersectingObject(Body.class) != null)
        {
             int x1 = ((FlagWarWorld)getWorld()).getXJugador() ;
           int x2 = getX();
           
           if((x2-x1>=0 && direccion<0) || (x2-x1<=0 && direccion>0) )
           {
            
           move(direccion*10);
          // Greenfoot.stop();
           
           impact=true;
           
           getWorld().addObject(new Explosion(),getX()+(50*direccion),getY());
           if(getOneIntersectingObject(Body.class) != null)
          {
           ((FlagWarWorld)getWorld()).setJugador(true);
          }
          else
          getWorld().removeObject(getOneIntersectingObject(PlayerShot.class));
            
        }
      }
      }
}
}